<?php
namespace app\admin\model;

use think\Model;
class Content extends Model
{

    public function getContentList($term_id){
        $Content_list = $this->where('term_id='.$term_id)->select();
        return $Content_list;
    }

    public function addContent($termId, $tagsValue){
        //待删除content表中的model_id列
        $data['model_id'] = D('Term')->getTermInfo($termId)['model_id'];

        if(!empty($tagsValue))
            $data['tags_value'] = $tagsValue;

        //$data['seo_title'] = I('post.seo_title');
        //$data['seo_keywords'] = I('post.seo_keywords');
        //$data['seo_description'] = I('post.seo_description');
        $data['cover_img'] = I('post.cover_img');
        $data['name'] = I('post.title');
        $data['intercept'] = I('?post.intercept')? I('post.intercept'): 0;
        $data['term_id'] = $termId;
        $data['keywords'] = I('post.keywords');
        $data['description'] = I('post.description');
        $data['content'] = I('post.content');
        $data['status'] = I('post.status');
        $data['rec_ok'] = I('post.rec_ok');
        $data['top_ok'] = I('post.top_ok');
        $data['create_time'] = strtotime(I('post.create_time'));
        $add_Content = $this->add($data);
        return $add_Content;
    }

    /**
     * 删除内容
     * @param mixed $id 待删除内容的ID，可以是数字或数组
     */
    public function delContent($id){
        $del_Content =$this->where(['id'=>$id])->delete();
        return $del_Content;
    }

    public function getInfo($id=null){
        if(empty($id))
            $id = I('get.id');
        $result = $this->find($id);
        $result['model_id'] = D('Term')->getTermInfo($result['term_id'])['model_id'];
        return $result;
    }


    public function postEdit($tagsValue){
        if(!empty($tagsValue))
            $data['tags_value'] = $tagsValue;
        $where['id'] = I('post.id');
        $data['model_id'] = I('post.model_id');
        if(0==I('post.term_id'));
        if(D('Term')->hasChild(I('post.term_id')))
            return false;
        $data['term_id'] = I('post.term_id');
        $data['intercept'] = I('?post.intercept')? I('post.intercept'): 0;
        $data['name'] = I('post.title');
        $data['keywords'] = I('post.keywords');
        $data['description'] = I('post.description');
        $data['content'] = I('post.content');
        $data['cover_img'] = I('post.cover_img');
        $data['status'] = I('post.status');
        $data['rec_ok'] = I('post.rec_ok');
        $data['top_ok'] = I('post.top_ok');
        //$data['seo_title'] = I('post.seo_title');
        //$data['seo_keywords'] = I('post.seo_keywords');
        //$data['seo_description'] = I('post.seo_description');
        $data['create_time'] = strtotime(I('post.create_time'));
        $post_edit = $this->where($where)->save($data);
        return $post_edit;
    }

    /**
     * 批量删除
     * 待废弃
     */
    public function delId($del_id){
        $map['id']  = array('in',$del_id);
        $delet_id = $this->where($map)->delete();
        return $delet_id;
    }

    public function listOrder($listorder,$listorder_id)
    {
        $data['listorder']=$listorder;
        $list_order = $this->where('id='.$listorder_id)->save($data);
        return $list_order;
    }

    //保留原有发布内容的形式，增加侧边栏发布内容功能
    public function addSliderContent()
    {

        $data['name'] = I('post.title');
        //$data['seo_title'] = I('post.seo_title');
        //$data['seo_keywords'] = I('post.seo_keywords');
        //$data['seo_description'] = I('post.seo_description');
        $data['intercept'] = I('?post.intercept')? I('post.intercept'): 0;
        $data['term_id'] = I('post.term_id');
        if(!empty($data['term_id']));
        if(D('Term')->hasChild($data['term_id']))
            return false;
        $data['model_id'] = D('Term')->getTermInfo($data['term_id'])['model_id'];
        $data['keywords'] = I('post.keywords');
        $data['description'] = I('post.description');
        $data['content'] = I('post.content');
        $data['status'] = I('post.status');
        $data['rec_ok'] = I('post.rec_ok');
        $data['top_ok'] = I('post.top_ok');
        $data['cover_img'] = I('post.cover_img');
        $data['create_time'] = strtotime(I('post.create_time'));
        $addSliderContent = $this->add($data);
        return $addSliderContent;
    }

    public function alterState($id, $field)
    {
        if(!$id)
            return false;
        $state = $this->find($id)[$field];
        $data[$field] = 1-$state;
        $result = $this->where(['id'=>$id])->save($data);
        $result = $result? $data[$field]: $state;
        return $result;
    }
}

